package com.lagou;

import com.lagou.bean.TestComponent;
import com.lagou.filter.TPMonitorFilter;
import org.apache.dubbo.common.utils.Holder;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.rpc.filter.TpsLimitFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.*;

/**
 * @author xsl
 * @date 2021/5/6 15:37
 * @Description: AnnotationConsumerMain
 */
public class AnnotationConsumerMain {

    private static final Logger logger = LoggerFactory.getLogger(AnnotationConsumerMain.class);
    private final static ThreadPoolExecutor executor = new ThreadPoolExecutor(150, 150, 60
            , TimeUnit.SECONDS, new SynchronousQueue<>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());

    private static int counter = 0;
    private final static ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        executorService.scheduleAtFixedRate(() -> {
            logger.error("======================================counter:" + counter);
            logger.error("=====================thread pool=================pool size:" + executor.getCorePoolSize() + ";active size:" + executor.getActiveCount());
            counter = 0;
            final Set<Map.Entry<String, Holder<Object>>> entries = TPMonitorFilter.monitorMap.entrySet();
            for (Map.Entry<String, Holder<Object>> entry : entries) {
                TPMonitorFilter.monitorMap.replace(entry.getKey(), null);
            }
        }, 1, 1, TimeUnit.MINUTES);
        TestComponent service = context.getBean(TestComponent.class);
        System.out.println("============================start===================================");
        while (true) {
//            System.in.read();

//            String resultA = service.methodA();
//            System.out.println("resultA:" + resultA);
//            String resultB = service.methodB();
//            System.out.println("resultB:" + resultB);
//            String resultC = service.methodC();
//            System.out.println("resultC:" + resultC);
            for (int i = 0; i < 200; i++) {
                counter++;
                try {
                    executor.execute(() -> {
                        service.methodA();
                        service.methodB();
                        service.methodC();
                    });
                } catch (Exception e) {
                    logger.error("message:" + e.getMessage() + e);
                }
            }
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "com.lagou.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}
